package com.lzyyj.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.net.ssl.HttpsURLConnection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class URLHelper {

	private static final Log log = LogFactory.getLog(URLHelper.class);
	
	/**
	 * 请求URL，并返回请求内容
	 * @param urlAddr
	 * @param method
	 * @param charsetName
	 * @return
	 */
	public String requestUrl(String urlAddr, String method, String charsetName) {
		String strResult = null;

		if (charsetName==null||charsetName.length()==0){
			charsetName = "GBK";
		}
		
		try {
			java.net.URL url = new URL(urlAddr);
			URLConnection urlConnection = url.openConnection();
			if(urlConnection==null){
				log.error("无法获取URL请求："+urlAddr);
				return strResult;
			}
			urlConnection.setDoOutput(true);
			log.debug("urlConnection.class="+urlConnection.getClass());

			if (urlConnection instanceof HttpURLConnection){
				((HttpURLConnection)urlConnection).setRequestMethod(method);
			}else if(urlConnection instanceof HttpsURLConnection){
				((HttpsURLConnection)urlConnection).setRequestMethod(method);
			}
			String temp;
			final StringBuffer sb = new StringBuffer();

			BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), charsetName));
			while ((temp = reader.readLine()) != null) {
				sb.append(temp);
			}
			strResult = sb.toString();

			if (urlConnection != null) {
				if (urlConnection instanceof HttpURLConnection){
					((HttpURLConnection)urlConnection).disconnect();
				}else if(urlConnection instanceof HttpsURLConnection){
					((HttpsURLConnection)urlConnection).disconnect();
				}
			}
		} catch (Exception e) {
			System.out.println("系统出错" + e);
		}
		
		return strResult;
	}
	
	/**
	 * URL地址请求 舒俊
	 * 
	 * @param urlAddr
	 * @return 2014-1-16
	 */
	public String getUrl(String urlAddr, String charsetName) {
		String strResult = null;
		
		if (charsetName==null||charsetName.length()==0){
			charsetName = "GBK";
		}
		
		URL url;
		String temp;
		final StringBuffer sb = new StringBuffer();
		try {
			url = new URL(urlAddr);
			final BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), charsetName));// 读取网页全部内容
			while ((temp = in.readLine()) != null) {
				sb.append(temp);
			}
			in.close();
		} catch (final MalformedURLException me) {
			// 你输入的URL格式有问题！请仔细输入
			me.getMessage();
			log.error("你输入的URL格式有问题！请仔细输入:" + me.toString());
		} catch (final IOException e) {
			e.printStackTrace();
			log.error("异常：" + e.toString());
		}
		strResult = sb.toString();
		
		return strResult;
	}
	
	/**
	 * post请求 舒俊
	 * 
	 * @param urlAddr
	 * @param data
	 * @return 2014-1-16
	 */
	public String postURL(String urlAddr, String data, String charsetName) {
		
		if (charsetName==null||charsetName.length()==0){
			charsetName = "GBK";
		}

		try {
			URL url = new URL(urlAddr);
			try {
				HttpURLConnection http = (HttpURLConnection) url.openConnection();
				http.setRequestMethod("POST");
				http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
				http.setDoOutput(true);
				http.setDoInput(true);
				System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
				System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
				http.connect();
				if (data!=null){
					OutputStream os = http.getOutputStream();
					os.write(data.getBytes(charsetName));// 传入参数
					os.flush();
					os.close();
				}
				InputStream is = http.getInputStream();
				int size = is.available();
				byte[] jsonBytes = new byte[size];
				is.read(jsonBytes);
				String message = new String(jsonBytes, charsetName);
				return message;
			} catch (IOException e) {
				e.printStackTrace();
				log.error("异常：" + e.toString());
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
			log.error("异常：" + e.toString());
		}
		return "";
	}
}
